home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- chgxmitf.c : Creates/Uncreates transmittable ASCII files
- from either ASCII or BINARY files
-
- I personally put this together for use with Kermit since it may change bin-
- ary files while sending or receiving them. However, anyone can use this
- to safely transmit any type of file as long as you have this routine
- on the other end to decode the file back to the original position.
- This works on the following systems: Unix Version 5.0 on both Motorola
- and Xenix, and DOS compiled under Microsoft Version 5.1.
-
- 12/8/89 Martin Katz
-
- *************************************************************************/
-
- #include <stdio.h>
-
- #define ddig(c) ((c) >= 0x0 && (c) <= 0x9)
- #define hdig(c) ((c) >= 0xa && (c) <= 0xf)
- #define MASK 0x0f
-
- #define dchr(c) ((c) >= '0' && (c) <= '9')
- #define hchr(c) ((c) >= 'A' && (c) <= 'F')
-
- #ifdef TWO60
- #define CLEAR printf("\033X")
- #else
- #define CLEAR printf("\033[H\033[2J\n")
- #endif
-
- #define BELL printf("\007\n")
-
- extern int errno;
-
- FILE *infile, *outfile; /* input & output files */
-
- unsigned long int textsize = 0, /* text size counter */
- codesize = 0, /* code size counter */
- printcount = 0; /* counter reporting progress every 1K bytes*/
-
-
- char *itohex(c,s)
- register unsigned char c;
- register char *s;
- {
- register char c1, c2;
-
- #ifdef DEBUG
- fprintf(stderr,"In itohex()...\n");
- fflush(stderr);
- #endif
-
- c1 = (c >> 4) & MASK;
- c2 = c & MASK;
-
- s[0] = ddig(c1) ? c1 + '0' : (hdig(c1) ? c1 - 0xa + 'A' : c1);
- s[1] = ddig(c2) ? c2 + '0' : (hdig(c2) ? c2 - 0xa + 'A' : c2);
- s[2] = '\0';
-
- return(s);
- }
-
- char hextoi(c1,c2)
- register char c1, c2;
- {
- register char i;
-
- #ifdef DEBUG
- fprintf(stderr,"In hextoi()...\n");
- fflush(stderr);
- #endif
-
- i = dchr(c1) ? c1 - '0' : (hchr(c1) ? c1 - 'A' + 0xa : c1);
- return (i << 4) + (dchr(c2) ? c2 - '0' : (hchr(c2) ? c2 - 'A' + 0xa : c2));
- }
-
- void Decode() /* Just the reverse of Encode(). */
- {
- unsigned long int len = 0;
- char in_buf[5];
- char hextoi();
-
- #ifdef DEBUG
- fprintf(stderr,"In Decode()...\n");
- fflush(stderr);
- #endif
-
- while ( fgets(in_buf,2,infile) != NULL )
- {
-
- if ( fgets(&in_buf[1],2,infile) == NULL )
- break;
-
- len+=2;
-
- textsize = len;
-
- putc(hextoi(in_buf[0],in_buf[1]),outfile);
-
- codesize++;
- printcount++;
-
- if (printcount > (unsigned long) 1024)
- {
- printf("%12ld\r", textsize);
- printcount = 0;
- /* Reports progress each time the textsize exceeds
- multiples of 1024. */
- }
- }
-
- printf("In : %ld bytes\n", textsize); /* Encoding is done. */
- printf("Out: %ld bytes\n", codesize);
- printf("Out/In: %.3f\n", (double)codesize / textsize);
- }
-
- void Encode()
- {
- unsigned long int in_cnt, read_len;
- char in_buf[1050], to_buf[5];
- char *itohex();
-
- #ifdef DEBUG
- fprintf(stderr,"In Encode()...\n");
- fflush(stderr);
- #endif
-
- while ( (read_len = fread(in_buf,1,1024,infile)) != 0 )
- {
- in_cnt = 0;
- while ( in_cnt < read_len )
- {
- fwrite (itohex(in_buf[in_cnt++],to_buf), 1, 2, outfile);
- textsize++;
- codesize+=2;
- printcount++;
- if (printcount > (unsigned long) 1024)
- {
- printf("%12ld\r", textsize);
- printcount = 0;
- /* Reports progress each time the textsize exceeds
- multiples of 1024. */
- }
- }
- }
-
- putc ('\n', outfile);
-
- printf("In : %ld bytes\n", textsize); /* Encoding is done. */
- printf("Out: %ld bytes\n", codesize);
- printf("Out/In: %.3f\n", (double)codesize / textsize);
-
- }
-
- void usage()
-
- {
- CLEAR;
- printf("chgxmitf: Four Arguments are required as exemplified:\n");
- printf("'chgxmitf c file1 file2' turns file1 into transmittable file2.\n");
- printf("'chgxmitf d file2 file1' turns transmittable file2 into actual file1.\n");
- BELL;
- }
-
- arg_check(argc, argv)
- int argc;
- char *argv[];
- {
- if (argc != 4)
- {
- usage();
- return(-1);
- }
-
- if ( strpbrk(argv[1], "DCdc") == NULL )
- {
- usage();
- return(-1);
- }
-
- if ( (strcmp(argv[3],argv[2]) == 0) && (strpbrk(argv[1], "Cc") != NULL) )
- {
- CLEAR;
- printf("chg_xmitfile: Cannot overlay a file with transmittable version.\n");
- BELL;
- return(-1);
- }
-
- return(0);
-
- }
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- char *to_file;
- char cmnd[100], real_to_file_name[15];
- short overlay_file = 0;
-
- if (arg_check(argc, argv))
- exit(-1);
-
- if ( strcmp(argv[3],argv[2]) == 0 )
- {
- overlay_file = 1;
- to_file = "interim_file";
- strcpy(real_to_file_name,argv[3]);
- }
- else
- to_file = argv[3];
-
-
- if (( infile = fopen(argv[2], "r+b")) == NULL )
- {
- CLEAR;
- printf("chg_xmitfile: unable to open from file %s\n", argv[2]);
- BELL;
- return(-1);
- }
-
- if (( outfile = fopen(to_file, "w+b")) == NULL )
- {
- CLEAR;
- printf("chg_xmitfile: unable to open to file %s\n", to_file);
- BELL;
- return(-1);
- }
-
- if (toupper(*argv[1]) == 'C')
- {
- printf("\n-->Changing %s into transmittable %s<--\n", argv[2], to_file);
- Encode();
- }
- else
- {
- printf("\n-->Creating actual file %s<--\n", to_file);
- Decode();
- }
-
- fclose(infile);
- fclose(outfile);
-
- if ( overlay_file == 1 )
- {
- sprintf(cmnd,"copy interim_file %s", real_to_file_name);
- system(cmnd);
- system("del interim_file");
- }
-
- return(0);
- }